<?php

namespace web\api\controller;

/**
 * Description of UserBase
 * 用户基类
 * @author zmh
 */
class UserBase extends ApiBase{

    /**
     * 用户id
     * @var type
     */
    protected $user_id = -1;
    protected $user_info = 0;
    protected $cacheLock = 0;
    protected $userM;

    const USDT_TYPE = 1; //USDT

    protected function _initialize() {
        parent::_initialize();
        $this->userM = new \addons\member\model\MemberAccountModel;
        $token = $this->_post('token');
        if(!$token) return $this->failJSON(401);//401token过期
        $this->user_info = $this->getGlobalCache($token); //redis中获取user_id
        if (!$this->user_info) {
           return $this->failJSON(401);//401token过期
        }
        if($this->user_info['secret_token']!=$this->getParamVal('secret_token')){
           return $this->failJSON(402);//402 秘钥错误,被登录
        }
        if((time()-$this->user_info['time'])>60000){
           return $this->failJSON(404);//长时间未操作
        }
        $this->is_frozen($this->user_info['id']);
        $this->user_id = $this->user_info['id'];
        $this->user_info['time'] = time();
        $this->setGlobalCache($this->user_info, $this->getParamVal('token')); //user_id存储到入redis
////        $request = \think\Request::instance();
////        $res = $this->cacheLock($request->module().$request->controller().$request->action().$this->user_id,5);
////        $this->cacheLock = $res;
         // $id = 1;
         // $this->user_id = $id;
//        $this->user_info = (new \addons\member\model\MemberAccountModel())->find($this->user_id);
    }

    protected function is_frozen($user_id) {
        $user_info = $this->userM->getDetail($user_id, "*");
        if (intval($user_info['is_frozen']) == 1) {
            return $this->failJSON(500);
        }
        $this->user_info = array_merge($this->user_info,$user_info);
    }


//    public function __destruct(){
//        if($this->cacheLock) $this->cacheLock['redis']->eval($this->cacheLock['script'],$this->cacheLock['data'],1);
//    }




}
